package com.gzc.just.play.last.war.loginserver.config;

import com.gzc.just.play.last.war.loginserver.sdk.model.UserPlatform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import jakarta.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * SDK配置类
 * 管理各SDK平台的配置信息
 */
@Configuration
@ConfigurationProperties(prefix = "sdk")
public class SDKConfig {
    private static final Logger logger = LoggerFactory.getLogger(SDKConfig.class);
    
    // 通用配置
    private boolean enabled = true;
    private int timeout = 5000; // 请求超时时间（毫秒）
    private int maxRetryCount = 3; // 最大重试次数
    
    // 平台配置
    private Map<String, PlatformConfig> platforms = new HashMap<>();
    
    @PostConstruct
    public void init() {
        // 初始化默认配置
        if (!platforms.containsKey("PC")) {
            platforms.put("PC", new PlatformConfig());
        }
        if (!platforms.containsKey("ANDROID")) {
            platforms.put("ANDROID", new PlatformConfig());
        }
        if (!platforms.containsKey("IOS")) {
            platforms.put("IOS", new PlatformConfig());
        }
        if (!platforms.containsKey("WECHAT")) {
            platforms.put("WECHAT", new PlatformConfig());
        }
        if (!platforms.containsKey("QQ")) {
            platforms.put("QQ", new PlatformConfig());
        }
        if (!platforms.containsKey("WEIBO")) {
            platforms.put("WEIBO", new PlatformConfig());
        }
        
        logger.info("SDKConfig initialized with {} platforms", platforms.size());
    }
    
    /**
     * 检查平台是否启用
     */
    public boolean isPlatformEnabled(UserPlatform platform) {
        if (!enabled) {
            return false;
        }
        
        PlatformConfig config = platforms.get(platform.getName());
        return config != null && config.isEnabled();
    }
    
    /**
     * 获取平台配置
     */
    public PlatformConfig getPlatformConfig(UserPlatform platform) {
        return platforms.get(platform.getName());
    }
    
    // Getters and Setters
    public boolean isEnabled() {
        return enabled;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    public int getTimeout() {
        return timeout;
    }
    
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    
    public int getMaxRetryCount() {
        return maxRetryCount;
    }
    
    public void setMaxRetryCount(int maxRetryCount) {
        this.maxRetryCount = maxRetryCount;
    }
    
    public Map<String, PlatformConfig> getPlatforms() {
        return platforms;
    }
    
    public void setPlatforms(Map<String, PlatformConfig> platforms) {
        this.platforms = platforms;
    }
    
    /**
     * 平台配置内部类
     */
    public static class PlatformConfig {
        private boolean enabled = true;
        private String appId;
        private String appKey;
        private String secret;
        private String redirectUri;
        private String apiBaseUrl;
        private int timeout = 5000;
        
        public PlatformConfig() {
        }
        
        public PlatformConfig(String appId, String appKey) {
            this.appId = appId;
            this.appKey = appKey;
        }
        
        // Getters and Setters
        public boolean isEnabled() {
            return enabled;
        }
        
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
        
        public String getAppId() {
            return appId;
        }
        
        public void setAppId(String appId) {
            this.appId = appId;
        }
        
        public String getAppKey() {
            return appKey;
        }
        
        public void setAppKey(String appKey) {
            this.appKey = appKey;
        }
        
        public String getSecret() {
            return secret;
        }
        
        public void setSecret(String secret) {
            this.secret = secret;
        }
        
        public String getRedirectUri() {
            return redirectUri;
        }
        
        public void setRedirectUri(String redirectUri) {
            this.redirectUri = redirectUri;
        }
        
        public String getApiBaseUrl() {
            return apiBaseUrl;
        }
        
        public void setApiBaseUrl(String apiBaseUrl) {
            this.apiBaseUrl = apiBaseUrl;
        }
        
        public int getTimeout() {
            return timeout;
        }
        
        public void setTimeout(int timeout) {
            this.timeout = timeout;
        }
    }
}